package com.xsy.carRental.controller;

import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.VehicleAddDTO;
import com.xsy.carRental.dto.VehiclePageQueryDTO;
import com.xsy.carRental.dto.VehicleUpdateDTO;
import com.xsy.carRental.entity.Vehicle;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.result.Result;
import com.xsy.carRental.service.VehicleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vehicles")
@Slf4j

public class VehicleController {

    @Autowired
    private VehicleService vehicleService;


    /**
     * 新增车辆
     *
     * @param vehicleAddDTO
     * @return
     */


    @PostMapping
    public Result add(@RequestBody VehicleAddDTO vehicleAddDTO) {
        log.info("前端传递过来的车辆信息:{}", vehicleAddDTO);
        vehicleService.add(vehicleAddDTO);
        return Result.success(MessageConstant.VEHICLEADD_SUCCESS);
    }

    /**
     * 条件可选分页查询车辆
     *
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping
    public Result<PageResult> pageQuery(VehiclePageQueryDTO vehiclePageQueryDTO) {
        log.info("条件可选分页查询车辆");
        PageResult pageResult = vehicleService.pageQuery(vehiclePageQueryDTO);

        return Result.success(pageResult);
    }


    /**
     * 更新车辆信息
     *
     * @param vehicleUpdateDTO
     */
    @PutMapping("/{vehicleId}")
    public Result update(@PathVariable int vehicleId, @RequestBody VehicleUpdateDTO vehicleUpdateDTO) {

        log.info("更新车辆信息 {}", vehicleUpdateDTO);
        vehicleUpdateDTO.setVehicleId(vehicleId);
        vehicleService.update(vehicleUpdateDTO);
        return Result.success();
    }

    /**
     * 根据id删除车辆
     *
     * @param vehicleId
     * @return
     */
    @DeleteMapping("/{vehicleId}")
    public Result delete(@PathVariable int vehicleId) {
        log.info("删除id为{}的车辆", vehicleId);
        vehicleService.delete(vehicleId);
        return Result.success();
    }

    /**
     * 根据id查询车辆详细信息
     *
     * @param vehicleId
     * @return
     */
    @GetMapping("/{vehicleId}")
    public Result<Vehicle> get(@PathVariable int vehicleId) {
        log.info("根据id查询车辆{}", vehicleId);
        Vehicle vehicle = vehicleService.getVehicleById(vehicleId);
        return Result.success(vehicle);
    }

}
